package com.ec.core.manage;

import java.util.*;
import java.sql.*;
import com.ec.core.db.*;

/**
 * manage the database connections from a connection pool;
 */
public class ConnectionManage
{
	private static com.ec.core.util.Logging  cat = com.ec.core.util.Logging.getLog("manage");

	// the system kidec
	private static ConnectionManage manage = null;

	private ConnectionPoolManager cpm;

	private String defaultAlias = "ec";
	
	private ConfigurationManage appConfig = null;

	private ConnectionManage()
	{
		initialize();
	}

	public static ConnectionManage getInstance()
	{
		if( manage != null) return manage;
		synchronized(ConnectionManage.class)
		{
			if( manage != null) return manage;
			manage = new ConnectionManage();
			return manage;
		}
	}

	public Connection getConnection(String alias) throws SQLException
	{
		return cpm.getPool(alias).getConnection();
	}

	public Connection getConnection() throws SQLException
	{
		return getConnection(defaultAlias);
	}

	private void initialize()
	{
		appConfig = com.ec.core.manage.ConfigurationManage.getInstance();
		int monitorInterval = 120;
		int dumplimit = 2000;
		String PREFIX0 = "dbpool.monitor";
		String PREFIX = "dbpool.db.connection";
		String s =  appConfig.getString(PREFIX0+".interval");
		if( s != null)
		{
			try
			{
				monitorInterval = Integer.parseInt(s);
			}
			catch(Exception e)
			{
				cat.error("connection.connectionmanager.monitor.interval format error",e);
			}
		}
		s = appConfig.getString(PREFIX0+".dumplimit");
		if( s != null)
		{
			try
			{
				dumplimit = Integer.parseInt(s);
			}
			catch(Exception e)
			{
				cat.error("connection.connectionmanager.monitor.dumplimit format error",e);
			}
		}
		String url_prefix = "";
		url_prefix = appConfig.getString(PREFIX0+".prefix");
		if( url_prefix == null || url_prefix.trim().length() == 0 )
			url_prefix = "jdbc:kidec:thin:connection:pool:";
                try{
                    cpm = new ConnectionPoolManager(url_prefix, monitorInterval,
                            dumplimit);
                    defaultAlias = appConfig.getString("dbpool.default");
                    cat.info("---> defaultAlias=" + defaultAlias);
                    List list = appConfig.getList(PREFIX+".enabled");
                    cat.info  ("---> ConnectionManage.java -> list.size() = "+list);
                    Iterator it = list.iterator();
                    int index = 0;
                    while (it.hasNext()) {
                        String enable =  (String)it.next();
                        if (enable == null || !enable.equals("true")) {
                        	index++;
                            continue;
                        }
                        String alias = appConfig.getString(PREFIX+"("+index+")[@alias]");
                        String driver = appConfig.getString(PREFIX+"("+index+").driver");
                        String url = appConfig.getString(PREFIX+"("+index+").url");
                        String user = appConfig.getString(PREFIX+"("+index+").user");
                        String password = appConfig.getString(PREFIX+"("+index+").password");
                        int maxCon = appConfig.getInteger(PREFIX+"("+index+").maxCon");
                        int idleTimeout = appConfig.getInteger(PREFIX+"("+index+").idleTimeout");
                        int checkoutTimeout = appConfig.getInteger(PREFIX+"("+index+").checkoutTimeout");
                        try {
                            cpm.addAlias(alias, driver, url, user, password,
                                         maxCon, idleTimeout, checkoutTimeout);
                            cat.info("ConnectionManage: create [" + alias + "," +
                                     url + "] successfully.");
                        } catch (Exception e) {
                            cat.error(" initilized the " + alias +
                                      " pool catch:", e);
                        }
                        index++;
                    }
                }catch(Exception e){
                    cat.error("initialize db connection pool occur error:",e);
                }
	}


	public String dumpInfo()
	{
		return cpm.dumpInfo();
	}

}
